package com.example.prom.ok.y2022.controller.email.aliyun.consumer;

import com.aliyun.mns.client.CloudAccount;
import com.aliyun.mns.client.CloudQueue;
import com.aliyun.mns.client.MNSClient;
import com.aliyun.mns.common.ClientException;
import com.aliyun.mns.common.ServiceException;
import com.aliyun.mns.model.Message;
import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.util.concurrent.*;

public class ComsumerDemo {

    private static final ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("dw-thread-call-runner-%d").build();
    private static final ThreadPoolExecutor threadPoolExecutor =  new ThreadPoolExecutor(10, 10, 3000L,
                                  TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1000),
                                    namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());

    public static void main(String[] args) {
        CloudAccount account = new CloudAccount(
                "LTAI5tAcQDUegjPA18jaLfH9",
                "ENEtjXQqCEfHCeMDla19LIRRAaggh4",
                "http://1559032671635936.mns.cn-hangzhou.aliyuncs.com");
//        CloudAccount account = new CloudAccount(
//                ServiceSettings.getMNSAccessKeyId(),
//                ServiceSettings.getMNSAccessKeySecret(),
//                ServiceSettings.getMNSAccountEndpoint());
        MNSClient client = account.getMNSClient(); //this client need only initialize once

        // Demo for receive message code
        try{
            CloudQueue queue = client.getQueueRef("common-support-email-queue");// replace with your queue name
            for (int i = 0; i < 10; i++) {
                CompletableFuture.runAsync(()->{
                    while (true){
                        Message popMsg = queue.popMessage();
                        System.out.println("threadName:"+Thread.currentThread().getName());
                        queue.deleteMessage(popMsg.getReceiptHandle());
                    }
                }, threadPoolExecutor);
            }

//            CloudQueue queue = client.getQueueRef("common-support-email-queue");// replace with your queue name
//                Message popMsg = queue.popMessage();
//                if (popMsg != null){
//                    System.out.println("message handle: " + popMsg.getReceiptHandle());
//                    byte[] messageBodyAsRawBytes = popMsg.getMessageBodyAsRawBytes();
//                    System.out.println(new String(messageBodyAsRawBytes));
//                    System.out.println("message id: " + popMsg.getMessageId());
//                    System.out.println("message dequeue count:" + popMsg.getDequeueCount());
//
////                    queue.deleteMessage(popMsg.getReceiptHandle());
//                    System.out.println("delete message successfully.\n");
//                }
        } catch (ClientException ce)
        {
            System.out.println("Something wrong with the network connection between client and MNS service."
                    + "Please check your network and DNS availablity.");
            ce.printStackTrace();
        } catch (ServiceException se)
        {
            if (se.getErrorCode().equals("QueueNotExist"))
            {
                System.out.println("Queue is not exist.Please create queue before use");
            } else if (se.getErrorCode().equals("TimeExpired"))
            {
                System.out.println("The request is time expired. Please check your local machine timeclock");
            }
            /*
            you can get more MNS service error code in following link.
            https://help.aliyun.com/document_detail/mns/api_reference/error_code/error_code.html?spm=5176.docmns/api_reference/error_code/error_response
            */
            se.printStackTrace();
        } catch (Exception e)
        {
            System.out.println("Unknown exception happened!");
            e.printStackTrace();
        }

        client.close();
    }
}
